<?php

declare(strict_types=1);

namespace Dreamcat\ApiDocTest\Demo\Route;

use Dreamcat\ApiDoc\Popo\RouterEntranceDefinition;
use ReflectionException;
use ReflectionMethod;

/**
 * 路由的测试用例
 * @author vijay
 */
class TestCaseFactory
{
    private static array $list;

    /**
     * @throws ReflectionException
     */
    public static function getList(): array
    {
        if (isset(self::$list)) {
            return self::$list;
        }
        self::$list = [];
        $dir = __DIR__ . "/../../../resources/routes/";
        foreach (scandir($dir) as $file) {
            if (!str_ends_with($file, ".json")) {
                continue;
            }
            $content = json_decode(file_get_contents("{$dir}{$file}"), true);
            $routeDef = new RouterEntranceDefinition();
            $routeDef->setUri($content["route"]["uri"])
                ->setMethod($content["route"]["method"])
                ->setAction(new ReflectionMethod($content["route"]["class"], $content["route"]["function"]));
            self::$list[] = [
                $routeDef,
                $content["expected"],
                $content["jsonMode"] ?? null,
            ];
        }
        return self::$list;
    }
}

# end of file
